package com.chuanggou.controller;

import com.chuanggou.file.FastDFSFile;
import com.chuanggou.util.FastDFSUtil;
import entity.Result;
import entity.StatusCode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author yd
 * @version 1.0
 * @date 2020/2/25 0025 黄啟军
 */
@RestController
@RequestMapping(value = "/upload")
public class FileUploadController {
    @PostMapping
    public Result upload(@RequestParam(value = "file")
                         MultipartFile file) throws Exception {
           //封装文件信息
        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(),//文件字节数组
                file.getBytes(),//文件名字
                StringUtils.getFilenameExtension(file.getOriginalFilename())//截取后缀，获取文件扩展名
                );
        //调用FastDFSUtil工具类将文件传入到FastDFS中
        String[] upload = FastDFSUtil.upload(fastDFSFile);
        //拼接访问地址url=http://192.168.43.113:8080
         // String url = "http://192.168.43.113:8080/"+upload[0]+"/"+upload[1];
        String url = FastDFSUtil.getTrackerInfo()+"/"+upload[0]+"/"+upload[1];
        return new Result(true, StatusCode.OK,"上传成功",url);
    }
}
